import React from 'react';
import { useHistory } from "react-router-dom";
import { Input, Button,Form,TextArea,Selector } from 'antd-mobile'
import '../css/add.css'
import request from "../utils/request";

export default ()=>{
    const history = useHistory();

    let author=localStorage.getItem('name')

    const onFinish = ({title,msg,type,author}) => {
        author = localStorage.getItem('name')
        request.post('/addmessage',{
            title,author,type,msg,
        }).then(({data})=>{
            if(data.success===true){
                // window.location.href='/login';
                history.push('/');

            }

        })
    }


    return (
        <>
            <Form

                layout='horizontal'
                onFinish={onFinish}
                footer={
                    <Button block
                            type='submit'
                            color='primary'
                    >
                        发布
                    </Button>
                }
            >
                <Form.Item
                    name='title'
                    label='标题'
                    rules={[{ required: true, message: '标题不能为空' }]}
                >
                    <Input placeholder='请输入标题' />
                </Form.Item>
                <Form.Item name='type'
                           label='分类'
                           rules={[{ required: true, message: '请选择发布的类别' }]}
                >
                    <Selector
                        columns={3}
                        options={[
                            { label: '最新发布', value: '最新发布' },
                            { label: '最新下载', value: '最新下载' },
                        ]}
                    />
                </Form.Item>
                <Form.Item
                    name='msg'
                    label='内容'
                    rules={[{ required: true, message: '标题不能为空' }]}
                >
                    <TextArea placeholder='请输入内容' maxLength={1000} autoSize showCount />
                </Form.Item>
            </Form>
        </>
    )
}